﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The concept balance filter can be used to match facts based on whether they have an @xbrli:balance attribute
    /// and, if they have that attribute, whether it has a value of debit or credit. 
    /// </summary>
    public class ConceptBalanceFilter : Filter
    {
        private string balance;

        /// <returns> The @balance attribute is the concept balance filter. </returns>
        public string Balance
        {
            get { return balance; }
            set { balance = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ConceptBalanceFilter(Linkbase.Resource resource)
            : base(resource)
        {
            balance = (string)resource.ResourceXElement.Attribute("balance") ?? string.Empty;    
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            this.dts = dts;

            List<FactItem> itemListTemp = new List<FactItem>();

            foreach (FactItem item in itemsToFilter)
            {
                if (Complement ? !(Balance == "none" ? item.Concept.BalanceString == "" : item.Concept.BalanceString.Equals(Balance)) : Balance == "none" ? item.Concept.BalanceString == "" : item.Concept.BalanceString.Equals(Balance))
                {
                    itemListTemp.Add(item);
                }
            }

            return itemListTemp;
        }
    }
}
